import { useState, useCallback, useEffect } from "react";

export const useThemeManager = () => {
  const [theme, setTheme] = useState<"light" | "dark">(() => {
    // 从本地存储中获取主题，如果没有则默认为'light'
    return (localStorage.getItem("theme") as "light" | "dark") || "light";
  });

  const toggleTheme = useCallback(() => {
    setTheme((prevTheme) => (prevTheme === "light" ? "dark" : "light"));
  }, []);

  useEffect(() => {
    // 当主题变化时，更新本地存储和文档类
    localStorage.setItem("theme", theme);
    document.documentElement.classList.toggle("dark", theme === "dark");
  }, [theme]);

  return { theme, toggleTheme };
};
